package com.lp.usercenter.controller;

import com.lp.usercenter.common.BaseResponse;
import com.lp.usercenter.common.ErrorCode;
import com.lp.usercenter.exception.BusinessException;
import com.lp.usercenter.model.domain.User;
import com.lp.usercenter.model.domain.request.UserRegisterRequest;
import com.lp.usercenter.service.UserService;
import com.lp.usercenter.utils.ResultUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 用户注册接口
     * @param userRegisterRequest 用户注册请求体
     * @return 用户id
     */
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest){
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        String planetCode = userRegisterRequest.getPlanetCode();
        if (StringUtils.isAllBlank(userAccount,userPassword,checkPassword)){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        long userId = userService.userRegister(userAccount, userPassword, checkPassword, planetCode);
        return ResultUtils.success(userId);
    }

    /**
     * 用户登录接口
     * @param userLoginRequest 用户登录的请求体
     * @param request 请求头
     * @return user信息
     */
    @PostMapping("/login")
    public BaseResponse<User> userLogin(@RequestBody UserRegisterRequest userLoginRequest, HttpServletRequest request){
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if (StringUtils.isAllBlank(userAccount,userPassword)){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        User user = userService.userLogin(userAccount, userPassword, request);
        return ResultUtils.success(user);
    }

    /**
     * 注销
     *
     * @param request 请求
     * @return 是否注销成功，成功返回1
     */
    @PostMapping("/logout")
    public BaseResponse<Integer> userLogout(HttpServletRequest request){
        if (request == null){
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        }
        Integer result = userService.userLogout(request);
        return ResultUtils.success(result);
    }

    /**
     * 查询用户信息(通过userName)
     *
     * @param userName 用户名
     * @param request 请求头
     * @return 用户信息
     */
    @GetMapping("/select")
    public BaseResponse<List<User>> selectUserByUserName(String userName, HttpServletRequest request){
        List<User> userList = userService.selectUserByUserName(userName, request);
        return ResultUtils.success(userList);
    }

    /**
     * 获取用户登录态
     * @param request 请求头
     * @return  user对象
     */
    @GetMapping("/current")
    private BaseResponse<User> getCurrentUser(HttpServletRequest request){
        User currentUser = userService.getCurrentUser(request);
        return ResultUtils.success(currentUser);
    }
}
